package com.zabra.zl.ui.verifyperson;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigman.wmzx.customcardview.library.CardView;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseActivity;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.VerifyPersonBean;
import com.zabra.zl.ui.success.VerifyPersonSuccessActivity;
import com.zabra.zl.ui.verifyperson.contract.VerifyPersonContract;
import com.zabra.zl.ui.verifyperson.presenter.VerifyPersonPresenter;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.HideUtil;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.SelectorUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 实名认证
 */
public class VerifyPersonActivity extends BaseMvpActivity<VerifyPersonPresenter> implements VerifyPersonContract.View {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_paerstype)
    TextView tvPaerstype;
    @BindView(R.id.rl_paerstype)
    RelativeLayout rlPaerstype;
    @BindView(R.id.et_papersid)
    EditText etPapersid;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_cardid)
    TextView tvCardid;
    @BindView(R.id.ll_verifed)
    CardView llVerifed;
    @BindView(R.id.ll_unverify)
    LinearLayout llUnverify;
    private List<String> list = new ArrayList<>();
    private int statePersion;
    private String strName, strIDcard;
    private Dialog dialog;

    /**
     * @return 如果没有实名 llVerifed 显示llUnverify
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_verify_person;
    }

    @Override
    public void initView() {
        mPresenter = new VerifyPersonPresenter();
        mPresenter.attachView(this);
        tvTitle.setText("实名认证");


        statePersion = getIntent().getExtras().getInt("isbundle");
        if (statePersion == 1) {
            //已经实名
            llVerifed.setVisibility(View.VISIBLE);
            llUnverify.setVisibility(View.GONE);
            String username = SharedPreferencesUtil.getInstance(this).getSP("username");
            String idcard = SharedPreferencesUtil.getInstance(this).getSP("idcard");
            tvName.setText(HideUtil.firstText(username));
            tvCardid.setText(HideUtil.hideCardNo(idcard));
        } else {
            //未实名
            llVerifed.setVisibility(View.GONE);
            llUnverify.setVisibility(View.VISIBLE);
        }

        list.add("身份证");
        list.add("学生证");
        list.add("军官证");
    }


    @OnClick({R.id.back, R.id.rl_paerstype, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.rl_paerstype:
//                SelectorUtils.chooseType(this, tvPaerstype, list);
                break;
            case R.id.tv_submit:
                strName = etName.getText().toString().trim();
                strIDcard = etPapersid.getText().toString().trim();
                if (TextUtils.isEmpty(strName) || TextUtils.isEmpty(strIDcard)) {
                    ToastUtil.showToast(this, "请完善个人信息");
                } else {
                    keyList.add("debtorId");
                    keyList.add("userName");
                    keyList.add("debtorCardId");
                    valueList.add(strUserid);
                    valueList.add(strName);
                    valueList.add(strIDcard);
                    mPresenter.getUserInfo(MaptoJsonUtils.Simple(keyList, valueList));
                }

                break;
        }
    }


    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(VerifyPersonBean bean, String token) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            SharedPreferencesUtil.getInstance(this).putSP("token", token);
            toClass1(this, VerifyPersonSuccessActivity.class);
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }

    }
}
